package com.baizhi.routing;

import com.baizhi.util.RabbitMQUtil;
import com.rabbitmq.client.*;

import java.io.IOException;

public class RabbitConsume {
    public static void main(String[] args) throws IOException {
        //得到连接
        Connection connection = RabbitMQUtil.getConnection();

        System.out.println(connection);
        //创建通道
        Channel channel = connection.createChannel();

        //设置交换机
        channel.exchangeDeclare("logs","direct");

        //创建临时队列绑定交换机
        String queue = channel.queueDeclare().getQueue();

        //参数3：routingkey
        channel.queueBind(queue,"logs","error");
        channel.queueBind(queue,"logs","bb");

        //消费消息
        channel.basicConsume(queue,true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String s = new String(body);
                System.out.println("消费者1："+s);
            }
        });


    }
}
